import 'package:azlistview/azlistview.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:sinosheets/components/CountryPicker/CityModel.dart';
import 'package:sinosheets/main_config.dart';

class CountryPickerLogic extends GetxController {
  List countryList = [];
  List hotCountry = [];
  Map select = {};

  List<CityModel> cList = [];
  List<CityModel> hList = [];

  TextEditingController searchController = TextEditingController();

  //是否显示中国
  bool isShowChina = true;

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    countryList = Get.arguments['countryList'];
    hotCountry = Get.arguments['hotCountry'];
    select = Get.arguments['select'];
    isShowChina = Get.arguments['isShowChina'] ?? true;
    mapToModel();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  // Map数据转model数据
  void mapToModel() {
    cList.clear();
    hList.clear();
    // 1. 普通国家列表
    List<CityModel> allCountry = countryList.map((e) => CityModel.fromJson(e)).toList();
    // 2. 热门国家列表
    List<CityModel> allHot = hotCountry.map((e) => CityModel.fromJson(e)).toList();
    // 3. 过滤中国
    if (!isShowChina) {
      allCountry = allCountry.where((e) => e.code != 'CN' && e.nameEn.toLowerCase() != 'china').toList();
      allHot = allHot.where((e) => e.code != 'CN' && e.nameEn.toLowerCase() != 'china').toList();
    }
    cList = allCountry;
    hList = allHot;
    for (int i = 0, length = cList.length; i < length; i++) {
      String pinyin = PinyinHelper.getPinyinE(cList[i].nameEn);
      String tag = pinyin.substring(0, 1).toUpperCase();
      cList[i].namePinyin = pinyin;
      if (RegExp('[A-Z]').hasMatch(tag)) {
        cList[i].tagIndex = tag;
      } else {
        cList[i].tagIndex = '#';
      }
    }
    for (int i = 0, length = hList.length; i < length; i++) {
      hList[i].tagIndex = 'Hot';
    }

    // A-Z sort.
    SuspensionUtil.sortListBySuspensionTag(cList);

    // show sus tag.
    SuspensionUtil.setShowSuspensionStatus(cList);
    cList.insertAll(0, hList);
    addSelectFlag();
  }

  //给国家列表添加是否选中标记
  void addSelectFlag() {
    for (var i in cList) {
      i.select = false;
      if (select['nameEn'] == i.nameEn && select['code'] == i.code) {
        i.select = true;
      }
    }
    for (var i in hList) {
      i.select = false;
      if (select['nameEn'] == i.nameEn && select['code'] == i.code) {
        i.select = true;
      }
    }
    update();
  }

  // 选中并返回国家数据
  void selectCountry(Map item) async {
    for (var i in countryList) {
      i['select'] = false;
    }
    for (var i in hotCountry) {
      i['select'] = false;
    }
    item['select'] = true;
    update();
    Get.back(result: item);
  }

  //搜索国家
  bool isSearch = false;
  List<CityModel> searchList = [];
  void searchCountry(String value) {
    searchList = [];
    if (searchController.text.isEmpty) {
      GBUtils.hideKeyboard();
      mapToModel();
      isSearch = false;
      update();
    } else {
      // 搜索时也要根据isShowChina过滤
      for (var i in cList) {
        if (!isShowChina && (i.code == 'CN' || i.nameEn.toLowerCase() == 'china')) {
          continue;
        }
        if (i.nameEn.toLowerCase().contains(searchController.text.toLowerCase()) || i.name.toLowerCase().contains(searchController.text.toLowerCase())) {
          searchList.add(i);
        }
      }
      isSearch = true;
      update();
    }
  }
}
